﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.IO;

using System.Xml;

using Microsoft.SharePoint;

namespace SO.SharePoint.CommandLineTool
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                bool export = false;

                if (args != null)
                {
                    if (args.Length == 1)
                    {
                        if (args[0].ToUpper() == "IMPORT")
                        {
                            export = false;
                        }
                        else if (args[0].ToUpper() == "EXPORT")
                        {
                            export = true;
                        }
                    }
                }

                string siteUrl = ConfigurationManager.AppSettings["SiteUrl"];
                string webUrl = ConfigurationManager.AppSettings["WebUrl"];
                string listName = ConfigurationManager.AppSettings["ListName"];

                SPSite site = new SPSite(siteUrl);
                if (site != null)
                {
                    SPWeb web = site.OpenWeb(webUrl);
                    if (web != null)
                    {
                        SPList list = web.Lists[listName];

                        if (list != null)
                        {
                            if (export)
                            {
                                StreamWriter sw;
                                foreach (SPView view in list.Views)
                                {
                                    sw = new StreamWriter(System.Environment.CurrentDirectory + "\\" + view.Title + ".xml");
                                    sw.Write(view.GetViewXml());
                                    sw.Close();
                                }
                            }
                            else
                            {
                                string[] files = Directory.GetFiles(System.Environment.CurrentDirectory, "*.xml");
                                foreach (string file in files)
                                {
                                    XmlDocument data = new XmlDocument();
                                    data.Load(file);
                                    SPView view = new SPView(list, data);
                                    
                                    System.Collections.Specialized.StringCollection fields = new System.Collections.Specialized.StringCollection();
                                    for(int i=0; i<view.ViewFields.Count; i++)                                    
                                    {
                                        fields.Add(view.ViewFields[i].ToString());
                                    }
                                    
                                    SPView newView = list.Views.Add(view.Title, fields, view.Query, view.RowLimit, view.Paged, view.DefaultView);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString() + "\r\n" + ex.StackTrace);
            }
        }
    }
}
